package com.fenghongzhang.kuaishou.mvp.login.presenter;



import android.util.Log;

import com.fenghongzhang.kuaishou.common.Constant;
import com.fenghongzhang.kuaishou.mvp.login.contract.LoginContract;
import com.fenghongzhang.kuaishou.mvp.login.entity.UserInfoBean;
import com.fenghongzhang.mvp_lib.presenter.BasePresenter;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginPresenter extends BasePresenter<LoginContract.ILoginModel, LoginContract.ILoginView> {

    private static final String TAG = "LoginPresenter";
    public LoginPresenter(LoginContract.ILoginModel mModel, LoginContract.ILoginView mView) {
        super(mModel, mView);
    }

    public void login(){
        mModel.login(mView.getName(), mView.getPwd(), new Observer<UserInfoBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserInfoBean loginInfoBean) {
                int code = loginInfoBean.getCode();
                if(code == Constant.SUCCESS){
                    Log.i(TAG, "onNext: 登录成功!");
                    mView.isLoginOk(loginInfoBean);
                }else{
                    Log.i(TAG, "onNext: 登录失败!");
                    mView.showMsg(loginInfoBean.getMsg());
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: "+e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
